/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved. 
 */
package com.icinfo.cloud.provider.punish.csource.manage.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.icinfo.cloud.provider.common.exception.BusinessException;
import com.icinfo.cloud.provider.peripheralinterface.xzzf.service.IXzzfInterfaceService;
import com.icinfo.cloud.provider.punish.common.caseregister.service.IRegisterCommonService;
import com.icinfo.cloud.provider.punish.csource.manage.dto.CaseSourceHandlePersonRecordSaveDto;
import com.icinfo.cloud.provider.punish.csource.manage.mapper.CaseSourceHandlePersonRecordMapper;
import com.icinfo.cloud.provider.punish.csource.manage.model.CaseSourceHandlePersonRecord;
import com.icinfo.cloud.provider.punish.csource.manage.service.ICaseSourceHandlePersonRecordService;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import com.icinfo.framework.tools.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 描述: 案源办案人员记录表-TRJ case_source_handle_person_record 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年08月17日
 */
@Service
public class CaseSourceHandlePersonRecordServiceImpl extends MyBatisServiceSupport implements ICaseSourceHandlePersonRecordService {
    /**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(CaseSourceHandlePersonRecordServiceImpl.class);

    @Autowired
    private CaseSourceHandlePersonRecordMapper caseSourceHandlePersonRecordMapper;
    @Resource
    private IXzzfInterfaceService xzzfInterfaceService;
    @Resource
    private IRegisterCommonService commonService;
    @Value("${isCheckHandleSign}")
    private String isCheckHandleSign;
    /**
     * 获取案源办案人员
     * @author liyafeng
     * @date 2022/8/17
      * @param caseSourceId
     * @return {@link List< CaseSourceHandlePersonRecord>}
     */
    @Override
    public List<CaseSourceHandlePersonRecord> getCaseSourceHandlePersonRecordList(String caseSourceId) {
        Example example=new Example(CaseSourceHandlePersonRecord.class);
        example.createCriteria().andEqualTo("caseSourceId",caseSourceId);
        List<CaseSourceHandlePersonRecord> caseSourceHandlePersonRecords=caseSourceHandlePersonRecordMapper.selectByExample(example);

        return caseSourceHandlePersonRecords;
    }
    /**
     * 保存案源办案人员信息
     * @author liyafeng
     * @date 2022/8/17
      * @param dto
     * @return
     */
    @Override
    public void saveCaseSourceHandlePersonRecord(CaseSourceHandlePersonRecordSaveDto dto) {
        CaseSourceHandlePersonRecord caseSourceHandlePersonRecord = new CaseSourceHandlePersonRecord();
        BeanUtils.copyProperties(dto,caseSourceHandlePersonRecord);
        String handlePersonId=dto.getHandlePersonId();
        if (StringUtils.isNotBlank(handlePersonId)) {
            if (ObjectUtil.isEmpty(xzzfInterfaceService.getLawEnforcementCertWithUserId(handlePersonId))) {
                throw new BusinessException("办案人员" + dto.getHandlePersonName() + "的执法证为空!");
            }
            caseSourceHandlePersonRecord.setLawEnforcementCert(xzzfInterfaceService.getLawEnforcementCertWithUserId(handlePersonId));
        }
        //  签名校验
        String handlePersonSignPic = commonService.getHandleSignData(handlePersonId);
        if ("true".equals(isCheckHandleSign)) {
            if (ObjectUtil.isEmpty(handlePersonSignPic)) {
                throw new BusinessException("办案人员" + dto.getHandlePersonName() + "的签名为空!");
            }
        }
        caseSourceHandlePersonRecord.setHandleSignPic(handlePersonSignPic);
        caseSourceHandlePersonRecord.setId(StringUtils.uuid());
        caseSourceHandlePersonRecordMapper.insertSelective(caseSourceHandlePersonRecord);
    }
}